package com.zkx.mvp.utils;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zkx.mvp.R;

/**
 * Author：zhangkx on 2017/6/16 17:20
 * Email：zkx9025@163.com
 * Toast 工具类
 */

public class ToastUtil {
    /** 上下文. */
    private static Context mContext = null;

    /** 显示Toast. */
    public static final int SHOW_TOAST = 0;

    /**
     * 主要Handler类，在线程中可用 what：0.提示文本信息
     */
    private static Handler baseHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SHOW_TOAST:
                    showToast(mContext, msg.getData().getString("TEXT"));
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 描述：Toast提示文本.
     *
     * @param text
     *            文本
     */
    public static void showToast(Context context, String text) {
        mContext = context;
        if (!StringUtil.isEmpty(text)) {
            Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
        }

    }

    /**
     * 描述：Toast提示文本.
     *
     * @param resId
     *            文本的资源ID
     */
    public static void showToast(Context context, int resId) {
        mContext = context;
        Toast.makeText(context, "" + context.getResources().getText(resId), Toast.LENGTH_SHORT).show();
    }

    /**
     * 描述：在线程中提示文本信息.
     *
     * @param resId
     *            要提示的字符串资源ID，消息what值为0,
     */
    public static void showToastInThread(Context context, int resId) {
        mContext = context;
        Message msg = baseHandler.obtainMessage(SHOW_TOAST);
        Bundle bundle = new Bundle();
        bundle.putString("TEXT", context.getResources().getString(resId));
        msg.setData(bundle);
        baseHandler.sendMessage(msg);
    }

    /**
     * 描述：在线程中提示文本信息.
     *
     * @param text
     *            消息what值为0
     */
    public static void showToastInThread(Context context, String text) {
        mContext = context;
        Message msg = baseHandler.obtainMessage(SHOW_TOAST);
        Bundle bundle = new Bundle();
        bundle.putString("TEXT", text);
        msg.setData(bundle);
        baseHandler.sendMessage(msg);
    }

    /**
     * 自定义toast
     */
    public static Toast showCustomerToast(Context context,String text,int imageResouse,boolean isVisibility) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.toast_product_gg, null);
        ImageView imageview  = (ImageView) view.findViewById(R.id.imageview);
        imageview.setImageResource(imageResouse);
        TextView title = (TextView) view.findViewById(R.id.title);
        if (isVisibility) {
            TextView notice = (TextView) view.findViewById(R.id.notice);
            notice.setVisibility(View.VISIBLE);
        }
        title.setText(text);
        Toast toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setView(view);
        toast.show();
        return toast;
    }

    /**
     * 是否有网络连接
     *
     * @param context
     */
    public static void isWhatError(Context context) {
        if (!WifiUtil.isConnectivity(context)) {
            showToast(context, context.getResources().getString(R.string.net_error));
        } else {
            showToast(context, context.getResources().getString(R.string.intent_error));
        }
    }
}
